﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Expect.Core.Interfaces;
using Expect.Core.Errors;

namespace Expect.Core.Helpers
{
    /// <summary>
    /// Methods for resolving fixture as a fixture class
    /// </summary>
    public static class ResolveFixtureAsFixtureClassHelpers
    {
        /// <summary>
        /// Resolve fixture
        /// </summary>
        /// <param name="context"></param>
        /// <param name="fixture"></param>
        /// <returns></returns>
        public static IFixtureImplementation AsFixtureClass(this IExecutionContext context, FixtureDefinition fixture)
        {
            string fixturename = fixture[0][0].Text;
            string target = context.NameProvider.ElementName(fixture[0].NamesForFluent().ToArray());
            Type t = context.Catalog.GetFixture(target);
            if (t != null)
            {
                fixture.ClassName = t.Name;
                fixture.ClassType = t;
                //fixture.Arguments = new FixtureRow(fixture[0].);
                if (!context.ResolveOnly)
                {
                    fixture.InitializeImplementation(context);
                }
                fixture.Definition = fixture[0];
                fixture.Rows.RemoveAt(0); //Remove the first row that contains fixture class and arguments

                //fixture.Implementation.Resolve();
                return fixture.Implementation;
            }
            else
            {
                fixture.Status.AddError(
                        ElementNotFoundError.Fixture(target)
                    );
            }

            return null;
        } 
    }
}
